package com.skit.mine;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.gson.Gson;
import com.skit.common.base.BaseActivity;
import com.skit.common.bean.StatusResult;
import com.skit.common.bean.User;
import com.skit.common.config.RetrofitClient;
import com.skit.common.config.RouteConfig;
import com.skit.common.http.HttpUtils;
import com.skit.common.manager.UserManager;
import com.skit.common.utils.ToastUtils;
import com.skit.mine.databinding.UserLoginBinding;
import com.skit.common.retrofit.UserProtocol;
import com.skit.mine.span.HtmlTextSpan;
import com.skit.mine.ui.RegisterActivity;

import retrofit2.Call;

@Route(path = RouteConfig.UserRoute.USER_LOGIN)
public class LoginActivity extends BaseActivity {
    private static final String TAG = "LoginActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //初始化data bind,并设置Helper实例
        UserLoginBinding binding = DataBindingUtil.setContentView(this, R.layout.user_login);
        LoginViewModel loginViewModel = new ViewModelProvider(this).get(LoginViewModel.class);
        binding.setLoginViewModel(loginViewModel);
        binding.setLifecycleOwner(this);

        binding.toolbar.setNavigationIcon(getDrawable(R.drawable.ic_keyboard_backspace_black_24dp));
        setSupportActionBar(binding.toolbar);
        if (getSupportActionBar() != null) {
//            getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        setTitle("");
        //底部协议提示文字
        HtmlTextSpan htmlTextSpan = HtmlTextSpan.setHtmlText(binding.agreementTextView, R.string.user_agreement);
        htmlTextSpan.setOnClickListener(v -> {
            Toast.makeText(this, "点击协议", Toast.LENGTH_SHORT).show();
        });


        binding.setIntentRegister(v -> {
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(intent);
        });

        binding.login.setOnClickListener(v -> {
            if (binding.userEdittext.getText() == null || binding.userEdittext.getText().toString().isEmpty() || binding.userPassWord.getText() == null || binding.userPassWord.getText().toString().isEmpty()) {
                ToastUtils.showToast("用户名或密码为空");
                return;
            }
            login(binding.userEdittext.getText().toString(), binding.userPassWord.getText().toString());
        });

        binding.forgetPassword.setOnClickListener(v -> startActivity(new Intent(LoginActivity.this, RegisterActivity.class)));
    }

    public void login(@NonNull String account, @NonNull String password) {
        Log.d(TAG, "login");
        String device = Build.MODEL;
        UserProtocol userProtocol = RetrofitClient.getService(UserProtocol.class);
        ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在登录");
        progressDialog.setCancelable(false);
        progressDialog.show();

        Call<StatusResult<User>> call = userProtocol.login(account, password, device);
        HttpUtils.doRequest(call, result -> {
            Log.d(TAG, "getRequest: ");
            progressDialog.dismiss();
            if (result == null || !result.isSuccess()) {
//                    Exception e = new Exception(result.getMsg());
                if (result != null) ToastUtils.showToast(result.getMsg());
                return;
            }
            Log.d(TAG, "getRequest: " + result.getMsg());
            if (result.getData() != null) {
                Log.d(TAG, "getRequest: " + new Gson().toJson(result.getData()));
                UserManager.saveUser(result.getData());
                ToastUtils.showToast("登录成功");
                finish();
//                    ARouter.getInstance().build("/app/main").withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).navigation();
//                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
//                    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                    startActivity(intent);
            }
        });
    }
}
